package top.kaoshanji.p2ch06b;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import top.kaoshanji.p2ch06b.support.MyBean;
import top.kaoshanji.p2ch06b.support.ParentBean;
import top.kaoshanji.p2ch06b.support.Top;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;

/**
 * 代码清单 p2ch06b-05：getBeanInfo方法的使用示例
 * author kaoshanji
 * ^_^
 * create time 2025/1/19 15:52
 */
public class IntrospectorExample01 {

    private static final Logger logger = LogManager.getLogger(IntrospectorExample01.class);

    public static void main(String[] args) throws IntrospectionException {
        logger.info("...代码清单 p2ch06b-05：getBeanInfo方法的使用示例...");

        BeanInfo beanInfo = Introspector.getBeanInfo(MyBean.class);
        outputBeanInfo(beanInfo);

        BeanInfo beanInfo1 = Introspector.getBeanInfo(Top.class);
        outputBeanInfo(beanInfo1);

        BeanInfo beanInfo2 = Introspector.getBeanInfo(MyBean.class, ParentBean.class);
        outputBeanInfo(beanInfo2);

        BeanInfo beanInfo3 = Introspector.getBeanInfo(MyBean.class, Top.class, Introspector.IGNORE_ALL_BEANINFO);
        outputBeanInfo(beanInfo3);

    }

    public static void outputBeanInfo(BeanInfo beanInfo) {
        System.out.println("== START ==");
        PropertyDescriptor[] properties = beanInfo.getPropertyDescriptors();
        for (PropertyDescriptor pd: properties) {
            System.out.println(pd);
        }
        System.out.println("==  END ==");
    }



}
